/**
* memberController Module
*
* memberController
*/
angular.module('app.con.member', []).controller('MemberController', function($scope, $modal, MemberService) {
	// ngView部分高度自适应
	var window_height = window.innerHeight - document.getElementsByTagName('nav')[0].offsetHeight - document.getElementsByTagName('footer')[0].offsetHeight;
	document.getElementById('ngView').style.height = window_height + 'px';

	//人员列表
	MemberService.list().success(function(res) {
		$scope.memberlist = res;
	});

	//添加用户
	$scope.addMember = function() {
		addMemberModule = $modal.open({
			templateUrl: 'addMember.html',
			controller: 'AddMemberController'
		});
		addMemberModule.result.then(function(result) {
			console.log(result);
			$scope.memberlist.push(result);
		}, function(reason) {
			console.log(reason);
		});
	};

	//删除用户
	$scope.deleteMember = function(num, username) {
		console.log(username);
		$scope.memberlist.splice(num, 1);
		MemberService.delete({username: username}).success(function(res) {
			console.log(res);
		});
	};
});

angular.module('app.con.member.addMember', []).controller('AddMemberController', function($scope, $modalInstance, MemberService) {
	$scope.newMember = function() {
		if (!$scope.addMemberUsername) {
			$scope.addMemberUsernameStyle = true;
			return false;
		} else if (!$scope.addMemberPassword) {
			$scope.addMemberUsernameStyle = false;
			$scope.addMemberPasswordStyle = true;
			return false;
		} else if (!$scope.addMemberDepartment) {
			$scope.addMemberPasswordStyle = false;
			$scope.addMemberDepartmentStyle = true;
			return false;
		} else {
			var newMember = {
				username    : $scope.addMemberUsername,
				password    : $scope.addMemberPassword,
				department  : $scope.addMemberDepartment,
				reg_datetime: new Date()
			};

			MemberService.add(newMember).success(function(res) {
				console.log(res);
				console.log(newMember);
				$modalInstance.close(newMember);
			});
		};
	};
});

